package com.hhs.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.BaseLogsService;
import com.hhs.storage.service.BaseRoleService;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("role")
@Slf4j
public class BaseRoleController {
    @Resource
    private BaseLogsService baseLogsService;
    @Resource
    BaseRoleService roleService;
    @Resource
    BaseUserService baseUserService;
    @Resource
    HttpServletRequest request;
    @PostMapping(value = "table")
    public Map<String, Object> getAllPage(InputParam inputParam) {
        log.info("查询角色分页信息");
        baseLogsService.log("角色管理","查询",null);
        inputParam.mapParams("name","flag");
        QueryWrapper<BaseRole> ew = new QueryWrapper<BaseRole>();
        if(!StringUtil.iszNull(inputParam.getName())){
            ew.like("name_",inputParam.getName());
        }
        if(!StringUtil.iszNull(inputParam.getFlag())){
            ew.like("flag_",inputParam.getFlag());
        }
        String loginUserId = SysUtil.getLoginUserId(request);
        if(!"2".equals(loginUserId)){
            ew.eq("1",1).and(wrapper -> wrapper.eq("flag_", "student").or().eq("flag_", "teacher"));
        }
        ew.orderByDesc("create_time_");
        Page<BaseRole> result = roleService.page(new Page<BaseRole>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", result.getRecords());
        map.put("total",result.getTotal());
        map.put("code", 1);
        return map;

    }

    @RequestMapping(value = "getAll")
    public Map<String, Object> getAll() {
        log.info("查询全部角色");
        baseLogsService.log("角色管理","查询",null);
        List<BaseRole> result = roleService.list(new QueryWrapper<BaseRole>().orderByDesc("create_time_"));
        return ResponseInfo.success(result);

    }

    @PostMapping("edit")
    public Map<String, Object> edit(BaseRole baseRole) {
        if (org.springframework.util.StringUtils.isEmpty(baseRole.getId())){
            baseLogsService.log("角色管理","新增",null);

        }else {
            baseLogsService.log("角色管理","编辑",null);
        }
        return roleService.addOrEdit(baseRole);

    }

    @DeleteMapping(value = "{id}")
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除角色");
        baseLogsService.log("角色管理","删除",null);
        roleService.dele(id);
        return ResponseInfo.success(null);

    }
    @PostMapping(value = "menus")
    public Map<String, Object> putMenus( String roleId, String menuIds) {
        log.info("修改该角色的关联菜单");
        if(StringUtils.isEmpty(roleId)) {
            return ResponseInfo.error("角色不能为空");
        }
        roleService.putMenus(roleId,menuIds);
        return ResponseInfo.success(null);

    }

    @GetMapping(value = "getByUserId")
    public Map<String, Object> getByUserId(String userId) {
        if(StringUtils.isEmpty(userId)) {
            return ResponseInfo.error("userId不能为空");
        }
        return ResponseInfo.success(roleService.getByUserId(userId));

    }
    @GetMapping(value = "mine")
    public Map<String, Object> mine(HttpServletRequest request) {
        String userId = SysUtil.getLoginUserId(request);
        if(StringUtils.isEmpty(userId)) {
            return ResponseInfo.error("userId不能为空");
        }
        return ResponseInfo.success(roleService.getByUserId(userId));
    }
}

